<?php
class PermissionController extends ApplicationController {
  public function index() {
    $this->redirect_to(array('controller' => 'permission', 'action' => 'enlist'));
  }
  
  public function enlist() {
    $this->permissions = Permission::find('all');
  }
  
  public function add() {
    //TODO: Remove hack
    $params_obj = (isset($this->params['permission'])) ? $this->params['permission'] : array();
    $this->permission = new Permission($params_obj);
    if($this->request()->post()) {
      if($this->permission->save()) {
        $this->flash['notice'] = 'Saved new Permission';
        $this->redirect_to(array('controller' => 'permission', 'action' => 'enlist'));
      }  
    }
  }

  public function view() {
    $this->permission = Permission::find_by_id($this->params['id']);
  }
  
  public function edit() {
    $this->permission = Permission::find($this->params['id']);
  }
  public function update() {
    $permission = Permission::find($this->params['id']);
    $permission->set_attributes($this->params['permission']);
    if($permission->save()) {
      $this->flash['notice'] = "Updated Permission {$permission->id}";
      $this->redirect_to(array('controller' => 'permission', 'action' => 'enlist'));
    }
  }
  
  public function delete() {
    if(!$permission = Permission::find_by_id($this->params['id'])) {
      $this->flash['notice'] = "Could not find Permission with ID {$this->params['id']}";
      $this->redirect_to(array('controller' => 'permission', 'action' => 'enlist'));
    }
    
    if($permission->delete()) {
      $this->flash['notice'] = "Deleted Permission {$permission->id}";
    } else {
      $this->flash['notice'] = "Could not delete Permission {$permission->id}";
    } 
    $this->redirect_to(array('controller' => 'permission', 'action' => 'enlist')); 
  }  
}